window.onload = function() {
    initDate()
};

/*******************************/
var currDT;
var aryDay = new Array("日", "一", "二", "三", "四", "五", "六"); //显示星期
var lastDay; //页面显示的最后一天
var firstDay; //页面显示的第一天
var date = new Date; //获取完整日期
var year = date.getFullYear(); //获取当前年份
//    alert(year);
//初始化日期加载
function initDate(){
	
    currDT = new Date();
    var dw = currDT.getDay(); //从Date对象返回一周中的某一天(0~6)
    var tdDT; //日期

    //在表格中显示一周的日期
    var objTB = document.getElementById("mytable"); //取得表格对象
    for (var i = 0; i < 7; i++) {
    	tdDT = getDays()[i];
		dw = tdDT.getDay(); //星期几
        objTB.rows[0].cells[i].innerHTML ="<div><span>"+tdDT.getDate()+"</span></div>";//显示
        var newtime = '<input type="text" value="' + year + "-" + (tdDT.getMonth() + 1) + "-" + tdDT.getDate() + '">';
        $(".newtime").append(newtime);
        
		if (tdDT.toLocaleDateString() == currDT.toLocaleDateString()){
			objTB.rows[0].cells[i].setAttribute('class','selec');
            $(".datetime").find("input").val($(".newtime").find("input").eq(i).val())
        }
    }
    var num = $('.selec').index();
    var startDate = $('.newtime').find('input').eq(num).val();
    slf.ajax({
    	url : "/course/getSyllabusListBySchool",
    	type:'get',
    	param : {
			pageIndex : 1,
			pageSize : 20,
			startDate : startDate,
		},
		success : function(data){
			var course = data.obj.data;
			var html = "";
			if(data.obj.data.length<=0){
				html += '<img src="../img/zwsj.png" alt="img" style="width:100%">';				
			}	
			for(var i = 0; i < course.length ; i++){
				var icon
				if(!course[i].iocn){
					icon = slf.defau.courseIcon(course[i].syllabusId);
				}else{
					icon = course[i].iocn.split(",");
				}
				var icons = icon.split(",");
				if(course[i].hotState = 1){
					html += "<div class='w100b radius4 over-hide  imgrx posi-rela am-margin-top-sm bottom-boshow2 bg-fff'>";
				}else if(course[i].newState = 1){
					html += "<div class='w100b radius4 over-hide classimg imgzx posi-rela am-margin-top-sm bottom-boshow2 bg-fff'>";
				}
				html+="<ul id='article' class='am-avg-sm-1 zjdt_div_ul'>";
				html+="<li class='dis-flex am-padding-top-sm bod-bottom'>";
				html+='<div class="w25 textzhong">';
				html+='<div class="img_div radius4 over-hide">';
				html += "<img onclick=\"javascript:window.location.href='basepkg_details.html?syllabusId="+course[i].syllabusId +"'\" src='"+icons[0]+"' class='w100b' />";
				html+='</div>';
				html+='</div>';
				html+='<div class="am-margin-left-xs w75 flex1">';
				html+='<ul class="w100b am-avg-sm-1 kcxx">';
				html+='<li class="dis-flex"><span class="tilte w100b flex font14">'+course[i].courseName;
				if(course[i].vacancy > 0){
					if(course[i].orderState != -1){
						html += "<span class='text-28AD47 am-text-right font14 am-align-right am-margin-0'>"+slf.orderState[course[i].orderState]+"</span>";
					}
					var icons = icon.split(",");
					if(course[i].hotState = 1){
						html += "<div class='w100b radius4 over-hide  imgrx posi-rela am-margin-top-sm bottom-boshow2 bg-fff'>";
					}else if(course[i].newState = 1){
						html += "<div class='w100b radius4 over-hide classimg imgzx posi-rela am-margin-top-sm bottom-boshow2 bg-fff'>";
					}
					html+="<ul id='article' class='am-avg-sm-1 zjdt_div_ul'>";
					html+="<li class='dis-flex am-padding-top-sm bod-bottom'>";
					html+='<div class="w25 textzhong">';
					html+='<div class="img_div radius4 over-hide">';
					html += "<img onclick=\"javascript:window.location.href='basepkg_details.html?syllabusId="+course[i].syllabusId +"'\" src='"+icons[0]+"' class='w100b' />";
					html+='</div>';
					html+='</div>';
					html+='<div class="am-margin-left-xs w75 flex1">';
					html+='<ul class="w100b am-avg-sm-1 kcxx">';
					html+='<li class="dis-flex"><span class="tilte w100b flex font14">'+course[i].courseName;
					if(course[i].vacancy > 0){
						if(course[i].orderState != -1){
							html += "<span class='text-28AD47 am-text-right font14 am-align-right am-margin-0'>"+slf.orderState[course[i].orderState]+"</span>";
						}
					}else{
						html += "<span class='text-28AD47 am-text-right font14 am-align-right am-margin-0'>已满</span>";
					}
					html += "</li>";
					html += "<li>"+course[i].description+"</li>";
					html += "<li>上课地点："+course[i].schoolName+" "+course[i].classroomName+"</li>";
					html += "<li>上课时间："+course[i].startDate+"</li>";
					html+='</ul>';
					html+='</div>';
					html+='</li>';
					html+='</ul>';
					html+='<ul class="w100b am-avg-sm-1 kcxx kcxx1 am-padding-sm bg-fff posi-rela">';
					html+='<li><span class="am-align-left am-margin-0">距离开课：</span>';
					html += "<div class='yomibox' data='"+course[i].startDate+"'></div>";
					html += "</li>";
					html += "<li class='kcyu_libtn' syllabusId='"+course[i].syllabusId +"'>";
					if(course[i].vacancy > 0){
						if(course[i].syllabusState != 5){
							if(course[i].isSchoolId != 0){
								html += "<button class='am-btn am-btn-sm an-btn-999 radius20 am-margin-top-sm am-btn-block posi-rela'>不在校区</button>";
							} else {
								if(course[i].orderState == 1){
									html += "<button class='am-btn am-btn-sm am-btn-blue radius20 am-margin-top-sm am-btn-block posi-rela'>取消预约</button>";
								} else if(course[i].orderState == -1 && course[i].purchaseState != 0){
									html += "<button class='am-btn am-btn-sm am-btn-blue radius20 am-margin-top-sm am-btn-block posi-rela'>立即预约</button>";
								}else if(course[i].orderState == -1 && course[i].purchaseState == 0){
									html += "<button class='am-btn font12 am-btn-sm am-btn-red radius20 am-margin-top-sm am-btn-block posi-rela'>购买课程</button>";
								}
							}          
						}else{
							html += "<button class='am-btn am-btn-sm an-btn-999 radius20 am-margin-top-sm am-btn-block posi-rela'>已结束</button>";
						}
					}
					html += "</li>";
					html += "</ul></div>";
				}
			}
			$("#course").html(html);
			yomi.yomi();
		}
	});
}

//取得当前日期一周内的某一天
function getWeek(i) {
    var now = new Date();
    var n = now.getDay();
    var start = new Date();
    start.setDate(now.getDate() - n + i); //取得一周内的第一天、第二天、第三天...
    return start;
}
function getWeekss(i) {
    var now = new Date($("#doc-datepicker").val().replace(/-/g,   "/"));
    var n = now.getDay();
    var start = new Date();
    start.setDate(now.getDate() - n + i); //取得一周内的第一天、第二天、第三天...
    return start;
}

//取得当前日期一周内的七天
function getDays() {
    var days = new Array();
    for (var i = 1; i <= 7; i++) {
        days[i - 1] = getWeek(i);
    }
    return days;
}
function getDayss() {
    var days = new Array();
    for (var i = 1; i <= 7; i++) {
        days[i - 1] = getWeekss(i);
    }
    return days;
}

/**********************************************************************/
$("#mytable tr td").click(function() {
	var num = $(this).index();
	if(num < 7){
		$("#mytable tr td").removeAttr("class");
	    $(this).attr("class", "selec");
	    var num = $(this).index();
	    $(".datetime").find("input").val($(".newtime").find("input").eq(num).val());
	    var startDate = $('.newtime').find('input').eq(num).val();
	    slf.ajax({
	    	url : "/course/getSyllabusListBySchool",
	    	type:'get',
	    	param : {
				pageIndex : 1,
				pageSize : 20,
				startDate : startDate,
				// courseType : courseType
			},
			success : function(data){
				var course = data.obj.data;
				var html = "";
				if(data.obj.data.length<=0){
					html += '<img src="../img/zwsj.png" alt="img" style="width:100%">';				
				}	
				for(var i = 0; i < course.length ; i++){
					var icon
					if(!course[i].iocn){
						icon = slf.defau.courseIcon(course[i].syllabusId);
					}else{
						icon = course[i].iocn.split(",");
					}
					var icons = icon.split(",");
					if(course[i].hotState = 1){
						html += "<div class='w100b radius4 over-hide  imgrx posi-rela am-margin-top-sm bottom-boshow2 bg-fff'>";
					}else if(course[i].newState = 1){
						html += "<div class='w100b radius4 over-hide classimg imgzx posi-rela am-margin-top-sm bottom-boshow2 bg-fff'>";
					}
					html+="<ul id='article' class='am-avg-sm-1 zjdt_div_ul'>";
					html+="<li class='dis-flex am-padding-top-sm bod-bottom'>";
					html+='<div class="w25 textzhong">';
					html+='<div class="img_div radius4 over-hide">';
					html += "<img onclick=\"javascript:window.location.href='basepkg_details.html?syllabusId="+course[i].syllabusId +"'\" src='"+icons[0]+"' class='w100b' />";
					html+='</div>';
					html+='</div>';
					html+='<div class="am-margin-left-xs w75 flex1">';
					html+='<ul class="w100b am-avg-sm-1 kcxx">';
					html+='<li class="dis-flex"><span class="tilte w100b flex font14">'+course[i].courseName;
					if(course[i].vacancy > 0){
						if(course[i].orderState != -1){
							html += "<span class='text-28AD47 am-text-right font14 am-align-right am-margin-0'>"+slf.orderState[course[i].orderState]+"</span>";
						}
						var icons = icon.split(",");
						if(course[i].hotState = 1){
							html += "<div class='w100b radius4 over-hide  imgrx posi-rela am-margin-top-sm bottom-boshow2 bg-fff'>";
						}else if(course[i].newState = 1){
							html += "<div class='w100b radius4 over-hide classimg imgzx posi-rela am-margin-top-sm bottom-boshow2 bg-fff'>";
						}
						html+="<ul id='article' class='am-avg-sm-1 zjdt_div_ul'>";
						html+="<li class='dis-flex am-padding-top-sm bod-bottom'>";
						html+='<div class="w25 textzhong">';
						html+='<div class="img_div radius4 over-hide">';
						html += "<img onclick=\"javascript:window.location.href='basepkg_details.html?syllabusId="+course[i].syllabusId +"'\" src='"+icons[0]+"' class='w100b' />";
						html+='</div>';
						html+='</div>';
						html+='<div class="am-margin-left-xs w75 flex1">';
						html+='<ul class="w100b am-avg-sm-1 kcxx">';
						html+='<li class="dis-flex"><span class="tilte w100b flex font14">'+course[i].courseName;
						if(course[i].vacancy > 0){
							if(course[i].orderState != -1){
								html += "<span class='text-28AD47 am-text-right font14 am-align-right am-margin-0'>"+slf.orderState[course[i].orderState]+"</span>";
							}
						}else{
							html += "<span class='text-28AD47 am-text-right font14 am-align-right am-margin-0'>已满</span>";
						}
						html += "</li>";
						html += "<li>"+course[i].description+"</li>";
						html += "<li>上课地点："+course[i].schoolName+" "+course[i].classroomName+"</li>";
						html += "<li>上课时间："+course[i].startDate+"</li>";
						html+='</ul>';
						html+='</div>';
						html+='</li>';
						html+='</ul>';
						html+='<ul class="w100b am-avg-sm-1 kcxx kcxx1 am-padding-sm bg-fff posi-rela">';
						html+='<li><span class="am-align-left am-margin-0">距离开课：</span>';
						html += "<div class='yomibox' data='"+course[i].startDate+"'></div>";
						html += "</li>";
						html += "<li class='kcyu_libtn' syllabusId='"+course[i].syllabusId +"'>";
						if(course[i].vacancy > 0){
							if(course[i].syllabusState != 5){
								if(course[i].isSchoolId != 0){
									html += "<button class='am-btn am-btn-sm an-btn-999 radius20 am-margin-top-sm am-btn-block posi-rela'>不在校区</button>";
								} else {
									if(course[i].orderState == 1){
										html += "<button class='am-btn am-btn-sm am-btn-blue radius20 am-margin-top-sm am-btn-block posi-rela'>取消预约</button>";
									} else if(course[i].orderState == -1 && course[i].purchaseState != 0){
										html += "<button class='am-btn am-btn-sm am-btn-blue radius20 am-margin-top-sm am-btn-block posi-rela'>立即预约</button>";
									}else if(course[i].orderState == -1 && course[i].purchaseState == 0){
										html += "<button class='am-btn font12 am-btn-sm am-btn-red radius20 am-margin-top-sm am-btn-block posi-rela'>购买课程</button>";
									}
								}          
							}else{
								html += "<button class='am-btn am-btn-sm an-btn-999 radius20 am-margin-top-sm am-btn-block posi-rela'>已结束</button>";
							}
						}
						html += "</li>";
						html += "</ul></div>";
					}
				}
				$("#course").html(html);
				yomi.yomi();
			}
		});
	}
});

//点击星期时间
$("#mystable tr td").click(function(){
	var num = $(this).index();
	if(num < 7){
		$("#mytable tr td").removeAttr("class");
		$("#mytable tr").find("td").eq(num).attr("class","selec");
		$(".datetime").find("input").val($(".newtime").find("input").eq(num).val());
	    var startDate = $('.newtime').find('input').eq(num).val();
	    slf.ajax({
			url : "/course/getSyllabusListBySchool",
			param : {
				pageIndex : 1,
				pageSize : 20,
				startDate : startDate
				// courseType : courseType
			},
			success : function(data){
				// console.log(data,"data")
				var course = data.obj.data;
				var html = "";
				if(data.obj.data.length<=0){
					html += '<img src="../img/zwsj.png" alt="img" style="width:100%">';	
				}	
				for(var i = 0; i < course.length ; i++){
					var icon
					if(!course[i].iocn){
						icon = slf.defau.icon;
					}else{
						icon = course[i].iocn.split(",");
					}
					var icons = icon.split(",");
					if(course[i].hotState = 1){
						html += "<div class='w100b radius4 over-hide  imgrx posi-rela am-margin-top-sm bottom-boshow2 bg-fff'>";
					}else if(course[i].newState = 1){
						html += "<div class='w100b radius4 over-hide classimg imgzx posi-rela am-margin-top-sm bottom-boshow2 bg-fff'>";
					}
					html+="<ul id='article' class='am-avg-sm-1 zjdt_div_ul'>";
					html+="<li class='dis-flex am-padding-top-sm bod-bottom'>";
					html+='<div class="w25 textzhong">';
					html+='<div class="img_div radius4 over-hide">';
					html += "<img onclick=\"javascript:window.location.href='basepkg_details.html?syllabusId="+course[i].syllabusId +"'\" src='"+icons[0]+"' class='w100b' />";
					html+='</div>';
					html+='</div>';
					html+='<div class="am-margin-left-xs w75 flex1">';
					html+='<ul class="w100b am-avg-sm-1 kcxx">';
					html+='<li class="dis-flex"><span class="tilte w100b flex font14">'+course[i].courseName;
					if(course[i].vacancy > 0){
						if(course[i].orderState != -1){
							html += "<span class='text-28AD47 am-text-right font14 am-align-right am-margin-0'>"+slf.orderState[course[i].orderState]+"</span>";
						}
					}else{
						html += "<span class='text-28AD47 am-text-right font14 am-align-right am-margin-0'>已满</span>";
					}
					html += "</li>";
					html += "<li>"+course[i].description+"</li>";
					html += "<li>上课地点："+course[i].schoolName+" "+course[i].classroomName+"</li>";
					html += "<li>上课时间："+course[i].startDate+"</li>";
					html+='</ul>';
					html+='</div>';
					html+='</li>';
					html+='</ul>';
					html+='<ul class="w100b am-avg-sm-1 kcxx kcxx1 am-padding-sm bg-fff posi-rela">';
					html+='<li><span class="am-align-left am-margin-0">距离开课：</span>';
					html += "<div class='yomibox' data='"+course[i].startDate+"'></div>";
					html += "</li>";
					html += "<li class='kcyu_libtn' syllabusId='"+course[i].syllabusId +"'>";
					if(course[i].vacancy > 0){
						if(course[i].syllabusState != 5){
							if(course[i].isSchoolId != 0){
								html += "<button class='am-btn am-btn-sm an-btn-999 radius20 am-margin-top-sm am-btn-block posi-rela'>不在校区</button>";
							} else {
								if(course[i].orderState == 1){
									html += "<button class='am-btn am-btn-sm am-btn-blue radius20 am-margin-top-sm am-btn-block posi-rela'>取消预约</button>";
								} else if(course[i].orderState == -1 && course[i].purchaseState != 0){
									html += "<button class='am-btn am-btn-sm am-btn-blue radius20 am-margin-top-sm am-btn-block posi-rela'>立即预约</button>";
								}else if(course[i].orderState == -1 && course[i].purchaseState == 0){
									html += "<button class='am-btn font12 am-btn-sm am-btn-red radius20 am-margin-top-sm am-btn-block posi-rela'>购买课程</button>";
								}
							}          
						}else{
							html += "<button class='am-btn am-btn-sm an-btn-999 radius20 am-margin-top-sm am-btn-block posi-rela'>已结束</button>";
						}
					}
					html += "</li>";
					html += "</ul></div>";
				}
				$("#course").html(html);
				yomi.yomi();
			}
		})
	}
});
//模态框确认按钮
$('.default').on('change',function(){
	var startDate = $("#doc-datepicker").val();
	$('.selec').removeClass('selec');
	$(".newtime").html('');
	var today=new Date($("#doc-datepicker").val().replace(/-/g,   "/"));
	var year = today.getFullYear(); //获取当前年份
	
	var dw = today.getDay(); //从Date对象返回一周中的某一天(0~6) 显示的是当前日期位置 
	var tdDT; //日期
	//在表格中显示一周的日期
	var objTB = document.getElementById("mytable"); //取得表格对象
	for (var i = 0; i < 7; i++) {
	    	tdDT = getDayss()[i];
	    	dw = tdDT.getDay(); //星期几
	        dm = tdDT.getMonth() + 1;
	        dy = tdDT.getDate();
	        if(dm<10){
	            dm="0"+dm;
	        }
	        if(dy<10){
	            dy="0"+dy;
	        }
	        objTB.rows[0].cells[i].innerHTML ="";//先清空
	        objTB.rows[0].cells[i].innerHTML ="<div><span>"+tdDT.getDate()+"</span></div>";//显示
	        
	        var newtime = '<input type="text" value="' + year + "-" + dm + "-" + dy + '">';
	        $(".newtime").append(newtime);
	        
	       if (tdDT.toLocaleDateString() == today.toLocaleDateString()){
				objTB.rows[0].cells[i].setAttribute('class','selec');
	            $(".datetime").find("input").val($(".newtime").find("input").eq(i).val())
	        }
			
	    }
	
	
	var startDate = $("#doc-datepicker").val();
	if(startDate != ''){
		slf.ajax({
			url : "/course/getSyllabusListBySchool",
			param:{
				pageIndex : 1,
				pageSize : 20,
				startDate : startDate
			},
			success:function(data){
				var course = data.obj.data;
				var html = "";
				if(data.obj.data.length<=0){
					html += '<img src="../img/zwsj.png" alt="img" style="width:100%">';	
				}	
				for(var i = 0; i < course.length ; i++){
					var icon
					if(!course[i].iocn){
						icon = slf.defau.icon;
					}else{
						icon = course[i].iocn.split(",");
					}
					var icons = icon.split(",");
					if(course[i].hotState = 1){
						html += "<div class='w100b radius4 over-hide  imgrx posi-rela am-margin-top-sm bottom-boshow2 bg-fff'>";
					}else if(course[i].newState = 1){
						html += "<div class='w100b radius4 over-hide classimg imgzx posi-rela am-margin-top-sm bottom-boshow2 bg-fff'>";
					}
					html+="<ul id='article' class='am-avg-sm-1 zjdt_div_ul'>";
					html+="<li class='dis-flex am-padding-top-sm bod-bottom'>";
					html+='<div class="w25 textzhong">';
					html+='<div class="img_div radius4 over-hide">';
					html += "<img onclick=\"javascript:window.location.href='basepkg_details.html?syllabusId="+course[i].syllabusId +"'\" src='"+icons[0]+"' class='w100b' />";
					html+='</div>';
					html+='</div>';
					html+='<div class="am-margin-left-xs w75 flex1">';
					html+='<ul class="w100b am-avg-sm-1 kcxx">';
					html+='<li class="dis-flex"><span class="tilte w100b flex font14">'+course[i].courseName;
					if(course[i].vacancy > 0){
						if(course[i].orderState != -1){
							html += "<span class='text-28AD47 am-text-right font14 am-align-right am-margin-0'>"+slf.orderState[course[i].orderState]+"</span>";
						}
					}else{
						html += "<span class='text-28AD47 am-text-right font14 am-align-right am-margin-0'>已满</span>";
					}
					html += "</li>";
					html += "<li>"+course[i].description+"</li>";
					html += "<li>上课地点："+course[i].schoolName+" "+course[i].classroomName+"</li>";
					html += "<li>上课时间："+course[i].startDate+"</li>";
					html+='</ul>';
					html+='</div>';
					html+='</li>';
					html+='</ul>';
					html+='<ul class="w100b am-avg-sm-1 kcxx kcxx1 am-padding-sm bg-fff posi-rela">';
					html+='<li><span class="am-align-left am-margin-0">距离开课：</span>';
					html += "<div class='yomibox' data='"+course[i].startDate+"'></div>";
					html += "</li>";
					html += "<li class='kcyu_libtn' syllabusId='"+course[i].syllabusId +"'>";
					if(course[i].vacancy > 0){
						if(course[i].syllabusState != 5){
							if(course[i].isSchoolId != 0){
								html += "<button class='am-btn am-btn-sm an-btn-999 radius20 am-margin-top-sm am-btn-block posi-rela'>不在校区</button>";
							} else {
								if(course[i].orderState == 1){
									html += "<button class='am-btn am-btn-sm am-btn-blue radius20 am-margin-top-sm am-btn-block posi-rela'>取消预约</button>";
								} else if(course[i].orderState == -1 && course[i].purchaseState != 0){
									html += "<button class='am-btn am-btn-sm am-btn-blue radius20 am-margin-top-sm am-btn-block posi-rela'>立即预约</button>";
								}else if(course[i].orderState == -1 && course[i].purchaseState == 0){
									html += "<button class='am-btn font12 am-btn-sm am-btn-red radius20 am-margin-top-sm am-btn-block posi-rela'>购买课程</button>";
								}
							}          
						}else{
							html += "<button class='am-btn am-btn-sm an-btn-999 radius20 am-margin-top-sm am-btn-block posi-rela'>已结束</button>";
						}
					}
					html += "</li>";
					html += "</ul></div>";
				}
				$("#course").html(html);
				yomi.yomi();		
						
						
			}
			
		});
	}
})